﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web;
using kunukn.RecommendationEngine.AreaRecommendation.Code.Constants;
using kunukn.RecommendationEngine.AreaRecommendation.Code.Engine;
using kunukn.RecommendationEngine.AreaRecommendation.Code.Entities;

namespace kunukn.RecommendationEngine.AreaRecommendation.Code.Helpers
{
    public static class HtmlHelper
    {
        public static string ViewedItemsThisSession_Html()
        {
            var sb = new StringBuilder();
            
            var list = SessionHelper.GetViewedItems();
            sb.AppendFormat("Viewed items this session: [");
            foreach (var item in list)
            {
                sb.AppendFormat("{0}, ", item.Name);
            }
            sb.AppendFormat("]");
            return sb.ToString();
        }

        public static string GetCosine_Html(Item x, Item2Item i2i)
        {
            if (!x.IsValid())
            {
                return string.Empty;
            }
                
            var list = i2i.GetCosineList(x).OrderByDescending(item => item.Measure).ToList();
            var sb = new StringBuilder();
            foreach (var i in list)
            {
                sb.AppendFormat("cos({0},{1}): {2}{3}", i.Name, i.ItemOther.Name, i.Measure.ToString(CultureInfo.InvariantCulture), Html.br);
            }                

            return sb.ToString();
        }

        public static string GetJaccardIndex_Html(Item x, Item2Item i2i)
        {
            if (!x.IsValid())
            {
                return string.Empty;
            }                

            var list = i2i.GetJaccardIndexList(x).OrderByDescending(item => item.Measure).ToList();
            var sb = new StringBuilder();
            foreach (var i in list)
            {
                sb.AppendFormat("ji({0},{1}): {2}{3}", i.Name, i.ItemOther.Name, i.Measure.ToString(CultureInfo.InvariantCulture), Html.br);
            }
                
            return sb.ToString();
        }

        public static string GetVectors_Html(Item itema, Item2Item i2i)
        {
            if (!itema.IsValid())
            {
                return string.Empty;
            }                
            
            var sb = new StringBuilder();

            string a = i2i.GetVector(itema).ToStringList();
            sb.AppendFormat("{0} : {1}{2}", itema.Name, a, Html.Br(2));

            int itemIndex = itema.GetItemIndex();
            int count = i2i.GetCount();
            for (int i = 0; i < count; i++)
            {
                if (i == itemIndex) // don't add same
                {
                    continue;
                } 
                    
                Item itemb = i2i.GetItems()[i];
                string b = i2i.GetVector(itemb).ToStringList();
                sb.AppendFormat("{0} : {1}{2}", itemb.Name, b, Html.br);
            }

            return sb.ToString();
        }

        public static string GetMaxCount_Html(Item itema, Item2Item i2i)
        {
            if (!itema.IsValid())
            {
                return string.Empty;
            }
                
            List<ItemCount> list = i2i.GetAssociatedSorted(itema);
            var sb = new StringBuilder();
            foreach (ItemCount itemCount in list)
            {
                sb.AppendFormat("{0}: {1}{2}", itemCount.Name, itemCount.Count, Html.br);
            }
            return sb.ToString();
        }


        /// <summary>
        /// View the Item 2 Item array
        /// O(n*n) where n is items
        /// </summary>
        /// <returns></returns>
        public static string GetMatrix_Html(Item2Item i2i)
        {
            var sb = new StringBuilder();
            int i = 0;
            sb.AppendLine("<table class='matrix'>");

            sb.Append(Html.tr);

            sb.Append(Html.td);
            sb.Append(Html.td_);

            var itemss = i2i.GetItems();
            foreach (var item in itemss)
            {
                sb.AppendFormat(Html.tdBgColorFormat, item.Color);
                sb.Append(item.Name);
                sb.Append(Html.td_);
            }
            sb.Append(Html.tr_);

            int[][] datas = i2i.GetMatrix();
            foreach (int[] t in datas)
            {
                sb.Append(Html.tr);

                var item = i2i.GetItems()[i++];
                sb.AppendFormat(Html.tdBgColorFormat, item.Color);
                sb.Append(item.Name);
                sb.Append(Html.td_);

                foreach (int t1 in t)
                {
                    sb.Append(Html.td);
                    sb.Append(t1);
                    sb.Append(Html.td_);
                }
                sb.Append(Html.tr_);
            }
            sb.AppendLine("</table>");

            return sb.ToString();
        }


    }
}